package com.uenpay.im.ui.old.activity.transfer;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.database.FriendDB;
import com.qyx.android.entity.FriendEntity;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.pwd.SetPayPwdActivity;
import com.uenpay.im.ui.old.activity.wallet.ChargeActivity;
import com.uenpay.im.ui.old.activity.wallet.WalletHandle;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.utilities.EditTextUtils;
import com.uenpay.im.utilities.ToastUtil;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.im.weight.PayPwdDialog;
import com.uenpay.im.weight.RoundImageView;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-6 上午11:25:51
 * @function 转账给某人
 */
public class TransferActivity extends BaseActivity {

	private RoundImageView transfer_avatar_iv;
	private TextView transfer_name_tv, transfer_account_tv;
	private EditText remark_et, amount_et;
	private Button comfirm_btn;
	private String cust_id;
	private String cust_name;
	private String cust_account;
	private double myBalance;
	private int haspaypassword = -1;
	private FriendEntity friendEntity = null;
	private View loading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isFullScreen = true;
		super.onCreate(savedInstanceState);
	}

	@Override
	public View bindView() {
		return null;
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_transfer_layout;
	}

	@Override
	public void initView(View view) {
		cust_id = getIntent().getStringExtra("cust_id");
		cust_name = getIntent().getStringExtra("cust_name");
		cust_account = getIntent().getStringExtra("cust_account");
		if (!TextUtils.isEmpty(cust_id)) {
			friendEntity = FriendDB.getFriend(cust_id);
		}
		initView();
		initListener();
		getMyBalance();
	}

	protected void initView() {
		((TextView) findViewById(R.id.title_textview)).setText(R.string.transfer);
		transfer_avatar_iv = (RoundImageView) findViewById(R.id.transfer_avatar_iv);
		transfer_name_tv = (TextView) findViewById(R.id.transfer_name_tv);
		transfer_account_tv = (TextView) findViewById(R.id.transfer_account_tv);
		remark_et = (EditText) findViewById(R.id.remark_et);
		amount_et = (EditText) findViewById(R.id.amount_et);
		comfirm_btn = (Button) findViewById(R.id.comfirm_btn);

		if (friendEntity != null && !TextUtils.isEmpty(friendEntity.cust_id)) {
			transfer_name_tv.setText(friendEntity.nick_name);
			transfer_account_tv.setText(friendEntity.mobile);
		} else {
			transfer_name_tv.setText(cust_name);
			transfer_account_tv.setText(cust_account);
		}
		transfer_avatar_iv.SetUrl(APIConfiguration.getAvatarUrlNormal(cust_id, PacketType.PERSONAL));
		loading = findViewById(R.id.loading);
	}

	protected void initListener() {
		// 返回键点击
		findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		amount_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				String str = arg0.toString();
				EditTextUtils.inputDecimal(amount_et, str);
				checkBtnStatus();
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});

		remark_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				String content = remark_et.getText().toString();
				if (Utils.String_length(content) > 36) {
					remark_et.setText(Utils.getStringByLength(36, content));
					remark_et.setSelection(remark_et.getText().toString().length());
					return;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});

		findViewById(R.id.comfirm_btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String amount = amount_et.getText().toString();
				String memo = remark_et.getText().toString();
				if (!TextUtils.isEmpty(amount)) {
					double temp = Double.valueOf(amount);
					// if (temp > 10000) {
					// showMoreDialog();
					// } else {
					if (temp <= 0.0) {
						ToastUtil.showToast(getApplicationContext(), "金额不能小于0");
						return;
					}
					if (temp > myBalance) {
						showInsufficientBalanceDialog();
					} else {
						if (haspaypassword == 1) {
							showPayPwdDialog(amount, memo);
						} else {
							showSetPwdDialog();
						}
						// }
					}
				}
			}
		});
	}

	// private void showMoreDialog() {
	// DialogUtility.showSingleBtnDialog(TransferActivity.this,
	// R.string.crowdfund_max, R.string.comfirm, true,
	// new IOnBottomDialogListener() {
	//
	// @Override
	// public void onClicked() {
	//
	// }
	// });
	// }

	/**
	 * 余额不足提示
	 */
	private void showInsufficientBalanceDialog() {
		loading.setVisibility(View.GONE);
		DialogUtility.showDialog(TransferActivity.this, R.string.insufficient_balance, R.string.sure, R.string.cancel, true,
				new IOnBottomDialogListener() {

					@Override
					public void onClicked() {
						Intent intent = new Intent(TransferActivity.this, ChargeActivity.class);
						startActivityForResult(intent, 100);
					}
				});
	}

	private void showSetPwdDialog() {
		DialogUtility.showDialog(TransferActivity.this, R.string.not_set_pay_pwd, R.string.sure, R.string.cancel, true,
				new IOnBottomDialogListener() {

					@Override
					public void onClicked() {
						Intent intent = new Intent(TransferActivity.this, SetPayPwdActivity.class);
						startActivity(intent);
						finish();
					}
				});
	}

	private void checkBtnStatus() {
		String money = amount_et.getText().toString();
		if (!TextUtils.isEmpty(money)) {
			comfirm_btn.setEnabled(true);
			comfirm_btn.setBackgroundResource(R.drawable.button_bg_normal);
		} else {
			comfirm_btn.setBackgroundResource(R.drawable.button_bg_gray);
			comfirm_btn.setEnabled(false);
		}
	}

	/**
	 * 支付密码提示框
	 *
	 * @param amount
	 * @param memo
	 */
	private void showPayPwdDialog(final String amount, final String memo) {
		PayPwdDialog.showBottomRedDialog(TransferActivity.this, new PayPwdDialog.IPayPwdInputFinishListener() {

			@Override
			public void onPayPwdInputFinish(String payPwd) {
				transferToPersonal(amount, amount, payPwd, memo);
			}
		});
	}

	/**
	 * 转账目标金额为0.00
	 *
	 * @param amount
	 * @param targettotal
	 * @param paypassword
	 */
	private void transferToPersonal(String amount, String targettotal, String paypassword, String memo) {
		TransferHandle.transferToPersonal(cust_id, amount, "0.00", paypassword, memo, new TransferHandle.ITransferLisener() {

			@Override
			public void onTransferResult(int status, String msg) {
				if (!TextUtils.isEmpty(msg)) {
					IMApplication.showToast(msg);
				}
				if (status == HttpInvokeResultCode.RESULT_OK) {
					if (haspaypassword == 0) {
						showSetPwdDialog();
					} else {
						finish();
					}
				}
			}
		});
	}

	private void getMyBalance() {
		WalletHandle.getBalance(new WalletHandle.IGetBalanceResultListener() {

			@Override
			public void onGetBalanceResult(String balance, int status, int _haspaypassword) {
				if (status == HttpInvokeResultCode.RESULT_OK) {
					if (!TextUtils.isEmpty(balance)) {
						myBalance = Double.valueOf(balance);
					}
					haspaypassword = _haspaypassword;
				}
			}
		});
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}
}
